package com.gnerv.battle.boot.module.base.business.impl;

import com.gnerv.battle.boot.module.base.model.bo.ViewBO;
import com.gnerv.battle.boot.module.base.model.dto.ViewRoleDTO;
import com.gnerv.battle.boot.module.base.model.dto.ViewTreeDTO;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.module.base.business.ViewBusiness;
import com.gnerv.battle.boot.module.base.model.dto.ViewCreateDTO;
import com.gnerv.battle.boot.module.base.model.dto.ViewDTO;
import com.gnerv.battle.boot.module.base.model.dto.ViewUpdateDTO;
import com.gnerv.battle.boot.module.base.service.ViewService;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class ViewBusinessImpl implements ViewBusiness {

    private final ViewService viewService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(ViewCreateDTO viewCreateDTO) {
        ViewBO viewBO = viewCreateDTO.toViewBO();
        viewBO.setUkId(UUIDTools.lower());
        viewService.create(viewBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        viewService.deleteByUkId(ukId);
        viewService.unbindRole(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(ViewUpdateDTO viewUpdateDTO) {
        viewService.updateByUkId(viewUpdateDTO.toViewBO());
        return RestResult.ok();
    }

    @Override
    public RestResult<ViewDTO> selectByUkId(String ukId) {
        return RestResult.ok(viewService.selectByUkId(ukId));
    }

    @Override
    public RestResult<List<ViewDTO>> listByName(String name) {
        return RestResult.ok(viewService.listByName(name));
    }

    @Override
    public RestResult<List<ViewDTO>> listByParentUkId(String parentUkId) {
        List<ViewDTO> viewList = viewService.listByParentUkId(parentUkId);
        return RestResult.ok(viewList);
    }

    @Override
    public RestResult<List<ViewTreeDTO>> listTree(String parentUkId) {
        List<ViewTreeDTO> viewTreeDTOList = viewService.listTree(parentUkId);
        return RestResult.ok(viewTreeDTOList);
    }

    @Override
    public RestResult<Void> bindRole(ViewRoleDTO viewRoleDTO) {
        viewService.unbindRole(viewRoleDTO.getViewUkId());
        viewService.bindRole(viewRoleDTO.getViewUkId(), viewRoleDTO.getRoleUkIdList());
        return RestResult.ok();
    }

}
